package com.stars.services.impl.system;
import com.stars.entity.system.SysCompanyEntity;
import com.stars.services.impl.BaseConfigTreeServiceImpl;
import com.stars.services.system.SysCompanyConfigServices;
import com.stars.dao.mapper.system.SysCompanyMapper;
import com.stars.services.impl.BaseConfigServiceImpl;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.stars.common.ObjectCopyUtils;
import com.stars.common.base.BaseConstant;
import com.stars.common.base.ResultMsg;
import com.stars.common.tree.TreeJson;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
/**
 * @Title: 公司管理 业务逻辑
 * @author dj
 * @date 2019-10-10 20:48:07
 * @version V1.0
 *
 */
@Service
public class SysCompanyConfigServicesImpl  extends BaseConfigTreeServiceImpl<SysCompanyMapper , SysCompanyEntity> implements SysCompanyConfigServices {

    @Autowired
    private SysCompanyMapper sysCompanyMapper;

    /**
    * 添加菜单数据
    * @param command
    * @param map
    * @throws Exception
    */
    public void add(TreeJson command, Map<String,String> map) throws Exception{
        Gson gson = new Gson();
        if(StringUtils.isBlank(command.getAddJson())){
           return;
        }
        List<SysCompanyEntity> addList= gson.fromJson(command.getAddJson(), new TypeToken<List<SysCompanyEntity>>() {}.getType());
        for (SysCompanyEntity add : addList) {
           //不包含Ext情况不往下走
           if(add.getPid().indexOf("Ext") ==-1){
             SysCompanyEntity entity = toAddEntity(command, add);
             insert(entity);
             map.put(add.getId(),entity.getId());
             addParentId(add,entity.getId(),addList,command,map);
           }
        }
    }
      /**
      * 递归添加树形
      * @param addList
      * @param command
      */
      private void addParentId(SysCompanyEntity root,String parentId,List<SysCompanyEntity> addList,TreeJson command, Map<String,String> map) throws Exception{
          for(SysCompanyEntity c:addList){
             if(root.getId().equals(c.getPid())){
               SysCompanyEntity cc = toAddEntity(command, c);
               cc.setPid(parentId);
               insert(cc);
               map.put(c.getId(),cc.getId());
               addParentId(c,cc.getId(),addList,command,map);
             }
          }
          }
      private SysCompanyEntity toAddEntity(TreeJson command, SysCompanyEntity add) {
          SysCompanyEntity entity = new SysCompanyEntity();
           ObjectCopyUtils.copyPropertiesIf(add,entity, BaseConstant.EXCLUDES);
           entity.setCreateId(command.getCreateId());
           entity.setCreateName(command.getCreateName());
           return entity;
       }
       /**
       * 更新菜单数据
       * @param command
       * @param map
       * @throws Exception
       */
       public void update(TreeJson command,Map<String,String> map) throws Exception{
           Gson gson = new Gson();
           if(StringUtils.isBlank(command.getUpdateJson())){
               return;
           }
           List<SysCompanyEntity> updateList= gson.fromJson(command.getUpdateJson(), new TypeToken<List<SysCompanyEntity>>() {}.getType());
             for (SysCompanyEntity  updateTree : updateList) {
                 SysCompanyEntity  entity = sysCompanyMapper.selectByPrimaryKey(updateTree.getId());
                 if(null == entity){
                     return;
                 }
                 ObjectCopyUtils.copyPropertiesIf(updateTree,entity,BaseConstant.EXCLUDES);
                 entity.setLastUpdateId(command.getCreateId());
                 entity.setLastUpdateName(command.getCreateName());
                 entity.setLastUpdateDate(new Date());
                 //如果包含ext ，不往下走
                 if(StringUtils.isNotBlank(updateTree.getPid()) && updateTree.getPid().indexOf("Ext") ==0){
                    //如果包含
                    entity.setPid(map.get(updateTree.getPid()));
                 }
                 updateSelectiveById(entity);
             }
         }


}